package com.core.website.api;

import com.core.website.common.Result;
import com.core.website.common.ResultUtil;
import com.core.website.common.TableDataInfo;
import com.core.website.entity.Content;
import com.core.website.entity.DutyUser;
import com.core.website.entity.User;
import com.core.website.service.DutyUserService;
import com.github.pagehelper.PageHelper;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/dutyUserApi")
public class DutyUserApi {
    @Autowired
    private DutyUserService dutyUserService;
    @PostMapping("/addUser")
    public Result addUser(@RequestBody DutyUser dutyUser){
        return ResultUtil.success(dutyUserService.addUser(dutyUser));
    }
    @PostMapping("/updateUser")
    public Result updateUser(@RequestBody DutyUser dutyUser){
        return ResultUtil.success(dutyUserService.updateUserById(dutyUser));
    }
    @PostMapping("/delUser")
    public Result delUser(@RequestBody List<String> list){
        return ResultUtil.success(dutyUserService.delUser(list));
    }
    @PostMapping("/queryPage")
    public Result queryPage(@RequestBody DutyUser dutyUser){
        // 获得用户信息
        User subject = (User) SecurityUtils.getSubject().getPrincipal();
        if(null==subject){
            return ResultUtil.error(-1000,"用户未登录");
        }
        dutyUser.setUserName(subject.getUserName()); // 设置用户名 如果不是admin  需要查询可排班的人员信息
        PageHelper.startPage(dutyUser.getPageNum(), dutyUser.getPageSize());
        List<DutyUser> result=dutyUserService.queryPage(dutyUser);
        return ResultUtil.success(TableDataInfo.getDataTable(result));
    }
    @PostMapping("/queryList")
    public Result queryList(@RequestBody DutyUser dutyUser){
        List<DutyUser> result=dutyUserService.queryPage(dutyUser);
        return ResultUtil.success(result);
    }
}
